package com.sentinel;

import com.alibaba.csp.sentinel.slots.block.degrade.DegradeRule;
import com.alibaba.csp.sentinel.slots.block.degrade.DegradeRuleManager;
import com.alibaba.csp.sentinel.slots.block.flow.FlowRule;
import com.alibaba.csp.sentinel.slots.block.flow.FlowRuleManager;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

import java.util.ArrayList;
import java.util.List;

@SpringBootApplication
public class Demo01Main {
    public static void main(String[] args) {
        SpringApplication.run(Demo01Main.class,args);
        //定义规则 有不同类型的规则,每种类型规则由很多个
        List<FlowRule> flowRules=new ArrayList<>();
        FlowRule flowRule=new FlowRule();
        //规则 对象 需要最少3个属性
        //绑定的资源
        flowRule.setResource("sayHi");
        //qps角度|维度 count就是限流的阈值(上限)
        flowRule.setCount(1);
        //qps维度 0=并发维度 1=qps维度
        flowRule.setGrade(1);
        //放到数组
        flowRules.add(flowRule);
        //使用sentinel管理器加载这个规则
        FlowRuleManager.loadRules(flowRules);
        /*//熔断规则
        List<DegradeRule> degradeRules=new ArrayList<>();
        DegradeRule degradeRule=new DegradeRule();
        degradeRule.setResource("sayHi1");
        //...
        degradeRules.add(degradeRule);
        //加载
        DegradeRuleManager.loadRules(degradeRules);*/
        System.out.println("sentinel规则加载完毕");
    }
}
